package com.lastproject.ejb.util;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.UserDTO;
import com.lastproject.ejb.entities.User;

/**
 * Session Bean implementation class UserConversion
 */
@Stateless
@LocalBean
public class UserConversion {

	@PersistenceContext
	EntityManager entityManager;

    public UserConversion() {
        // TODO Auto-generated constructor stub
    }
    
    public UserDTO fromEntity(User user) {
    	UserDTO userDTO = new UserDTO();
    	
    	userDTO.setId(user.getId());
    	userDTO.setUsername(user.getUsername());
    	userDTO.setPassword(user.getPassword());
    	userDTO.setStatusPassword(user.getStatusPassword());
    	userDTO.setFirstname(user.getFirstname());
    	userDTO.setLastname(user.getLastname());
    	userDTO.setWorkEmail(user.getWorkEmail());
    	userDTO.setDob(user.getDob());
    	userDTO.setPersonalEmail(user.getPersonalEmail());
    	userDTO.setPersonalPhone(user.getPersonalPhone());
    	userDTO.setEmploymentDate(user.getEmploymentDate());
    	userDTO.setDepartureDate(user.getDepartureDate());
    	userDTO.setMedCheckUpDate(user.getMedCheckUpDate()); 
    	userDTO.setEmploymentType(user.getEmploymentType());
    	userDTO.setPosition(user.getPosition());
    	userDTO.setCv(user.getCv());
    	userDTO.setEnabled(user.isEnabled());
    	
    	return userDTO;
    }
    
    public User fromDTO(UserDTO userDTO) {
    	
    	User user = entityManager.find(User.class, userDTO.getId());
    	
    	if(user == null) {
    		user = new User();
    	}
    	
    	user.setId(userDTO.getId());
    	user.setUsername(userDTO.getUsername());
    	user.setPassword(userDTO.getPassword());
    	user.setStatusPassword(userDTO.getStatusPassword());
    	user.setFirstname(userDTO.getFirstname());
    	user.setLastname(userDTO.getLastname());
    	user.setWorkEmail(userDTO.getWorkEmail());
    	user.setDob(userDTO.getDob());
    	user.setPersonalEmail(userDTO.getPersonalEmail());
    	user.setPersonalPhone(userDTO.getPersonalPhone());
    	user.setEmploymentDate(userDTO.getEmploymentDate());
    	user.setDepartureDate(userDTO.getDepartureDate());
    	user.setMedCheckUpDate(userDTO.getMedCheckUpDate()); 
    	user.setEmploymentType(userDTO.getEmploymentType());
    	user.setPosition(userDTO.getPosition());
    	user.setCv(userDTO.getCv());
    	user.setEnabled(userDTO.isEnabled());
    	
    	return user;
    }

}
